﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using vensoft.Entities;
using System.Data;


namespace vensoft.DataAccess
{
    public class HistoricoDataAcess : MasterDataAcess,IHistoricoTransferenciaDataAccess
    {
         public HistoricoDataAcess()
            : base()
        {
           
        }

        public void RegistrarHistorico(Historico historico)
        {
            SqlCommand commando = base.CreateCommand("RegistrarHistorico");
            AddParameters(historico, commando);
        }

        private void AddParameters(Historico historico, SqlCommand command)
        {
            base.AddInParameter(command, "ID_Historico", historico.Id_Historico);
            base.AddInParameter(command, "ID_AlmOrigen", historico.ID_AlmOrigen);
            base.AddInParameter(command, "Ubic_Origen", historico.Ubic_Origen);
            base.AddInParameter(command, "ID_AlmDestino", historico.ID_AlmDestino);
            base.AddInParameter(command, "Ubic_Destino", historico.Ubic_Destino);
            base.AddInParameter(command, "Fecha", historico.Fecha);
            base.AddInParameter(command, "Motivo", historico.Motivo);
            base.AddInParameter(command, "ID_Prod", historico.ID_Prod);
            base.AddInParameter(command, "Cod_Prod", historico.Cod_Prod);
            base.AddInParameter(command, "Cantidad", historico.Cantidad);
            base.AddInParameter(command, "nomb_pers", historico.Nombre_Usuario);
            base.ExecuteCommand(command);

        }

        public List<Historico> GetAllFromHistorico()
        {
            SqlCommand comand = base.CreateSelectCommand("GetAllFromHistoricoTransferencia");
            return SelectObjectFactory(comand);
        }

        private List<Historico> SelectObjectFactory(SqlCommand comand)
        {
            List<Historico> lista = new List<Historico>();
            using (SqlDataReader reader = base.ExecuteReader(comand))
            {
                while (reader.Read())
                {
                    Historico historico = new Historico()
                        {
                            Id_Historico = reader.GetInt32(0),
                            Fecha = reader.GetDateTime(5),
                            Ubic_Origen = reader.GetString(2),
                            Ubic_Destino = reader.GetString(4),
                            Cod_Prod = reader.GetString(8),
                            Cantidad = reader.GetInt32(9),
                            Motivo = reader.GetString(6),
                            Nombre_Usuario = reader.GetString(10)
                        
                        };
                    lista.Add(historico);
                }
            }
            return lista;
        }

        public List<Historico> GetAllFromHistoricoByFechas(DateTime f_inicio, DateTime f_fin)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromHistoricoByFecha");
            command.Parameters.AddWithValue("f_inicio", f_inicio);
            command.Parameters.AddWithValue("f_fin", f_fin);
            return SelectObjectFactory(command);
        }

        public List<Historico> GetAllFromHistoricoByCodProd(string cod_prod)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromHistoricoByCodProd");
            command.Parameters.AddWithValue("cod_prod", cod_prod);            
            return SelectObjectFactory(command);
        }
    }
}
